﻿using System;
using System.Collections.Generic;
using System.Text;

namespace _4._9BookDemo
{
    class Book
    {
        public int BookId { get; }
        public string Name { get; }
        public double Price { get; }//价格
        public string Press { get; }//出版社
        public string Print { get; }//出版号
        public string Autor { get; }//作者     
        public Book(int bookId, string name, double price, string press, string print, string autor)
        {
            BookId = bookId;
            Name = name;
            Price = price;
            Press = press;
            Print = print;
            Autor = autor;
        }
        public override string ToString() => BookId + " " + Name + " " + Price + " " + Press + " " + Autor;

        
        public virtual string ToString(string format) => ToString(format, null);
        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "A":
                    return ToString();
                case "C":
                    return $"{ToString()}, Autor: {Autor}";

                case "P": return $"{Autor} {Name}";
                default:
                    throw new FormatException($"invalid format string {format}");
            }
        }
        public int CompareTo(Book other)
        {
            int compare = Name?.CompareTo(other?.Name) ?? -1;
            if (compare == 0)
            {
                return Autor?.CompareTo(other?.Autor) ?? -1; 
            }
            return compare;
        }
    }
}
